<?php	require_once(dirname(__FILE__).'/inc/config.inc.php');IsModelPriv('usermodel_list');

/*
**************************
(C)2010-2014 phpMyWind.com
update: 2014-5-30 13:41:11
person: Feng
**************************
*/


//初始化参数
$tbname   = '#@__usermodel';
$gourl    = 'usermodel.php';
$action   = isset($action)   ? $action   : '';
$infotype = isset($infotype) ? $infotype : '';
$checkinfo = isset($checkinfo) ? $checkinfo : 'false';



//添加自定义模型
if($action == 'add')
{

	//构成表前缀
	$modeltbname = $db_tablepre.'member_'.$modeltbname;


	$r = $dosql->GetOne("SELECT id FROM `$tbname` WHERE `modeltbname`='$modeltbname'");
	if($dosql->IsTable($modeltbname) or !empty($r['id']))
	{
		ShowMsg('模型表名已存在！',$gourl);
		exit();
	}
	
	$r = $dosql->GetOne("SELECT id FROM `$tbname` WHERE `modelname`='$modelname'");
	if(!empty($r['id']))
	{
		ShowMsg('模型标识已存在！',$gourl);
		exit();
	}


	$sql = "CREATE TABLE `$modeltbname` (
	`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户id',
				  PRIMARY KEY (`id`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	//添加模型
	if($dosql->ExecNoneQuery($sql))
	{
		$sql = "INSERT INTO `$tbname` (modelname, modeltitle, modeltbname, orderid, checkinfo) VALUES ('$modelname', '$modeltitle', '$modeltbname', '$orderid', '$checkinfo')";
		$dosql->ExecNoneQuery($sql);
		header("location:$gourl");
		exit();
	}
	else
	{
		ShowMsg("模型添加失败！请检查设置的内容是否合法！",'-1');
		exit();
	}
}


//修改自定义模型
else if($action == 'update')
{
	//初始化参数
	if(is_array($defaultfield)) $defaultfield = implode(',',$defaultfield);


	$sql = "UPDATE `$tbname` SET modeltitle='$modeltitle', orderid='$orderid', checkinfo='$checkinfo' WHERE id=$id";
	if($dosql->ExecNoneQuery($sql))
		header("location:$gourl");
	else
		ShowMsg("模型添加失败！请检查设置的内容是否合法！",'-1');

	exit();
}


//自定义模型状态
else if($action == 'check')
{
	if($checkinfo == 'true')
		$sql = "UPDATE `$tbname` SET checkinfo='false' WHERE id=$id";

	else if($checkinfo == 'false')
		$sql = "UPDATE `$tbname` SET checkinfo='true' WHERE id=$id";

	if($dosql->ExecNoneQuery($sql))
	{
		header("location:$gourl");
		exit();
	}
}


//删除自定义模型
else if($action == 'del')
{	
	if($id==1){
		ShowMsg('此模型不可删除！','-1');
		exit();
	}
	
	$r = $dosql->GetOne("SELECT `modeltbname` FROM `#@__usermodel` WHERE `id`=$id");
	if(isset($r) && is_array($r))
	{
		$dosql->ExecuteSafeQuery("DROP TABLE `".$r['modeltbname']."`");
	}
	$dosql->ExecNoneQuery("update `#@__member` SET modelid= '1' WHERE `modelid`=$id");
	$dosql->ExecNoneQuery("DELETE FROM `$tbname` WHERE `id`=$id");
	header("location:$gourl");
	exit();
}


//无条件返回
else
{
    header("location:$gourl");
	exit();
}
?>